package com.tistory.commons.utils;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractJExcelView;

import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * This class builds an Excel spreadsheet document using Apache POI library.
 * @author www.codejava.net
 *
 */
public class ExcelBuilder extends AbstractJExcelView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			WritableWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// get data model which is passed by the Spring container
		List<ExcelVo> listBooks = (List<ExcelVo>) model.get("listBooks");
		
		// create a new Excel sheet
		WritableSheet sheet = workbook.createSheet("입금요청 샘플", 0);
		
		// create header row
		sheet.addCell(new Label(0, 0, "순번"));
		sheet.addCell(new Label(1, 0, "요청일시"));
		sheet.addCell(new Label(2, 0, "사업자번호"));
		sheet.addCell(new Label(3, 0, "이체금액"));
		sheet.addCell(new Label(4, 0, "은행코드"));
		sheet.addCell(new Label(5, 0, "은행명"));
		sheet.addCell(new Label(6, 0, "계좌번호"));
		
		// create data rows
		int rowCount = 1;
		
		for (ExcelVo aBook : listBooks) {
			sheet.addCell(new Label(0, rowCount, aBook.getTitle()));
			sheet.addCell(new Label(1, rowCount, aBook.getAuthor()));
			sheet.addCell(new Label(2, rowCount, aBook.getIsbn()));
			sheet.addCell(new Label(3, rowCount, aBook.getPublishedDate()));
			sheet.addCell(new jxl.write.Number(4, rowCount, aBook.getPrice()));
			sheet.addCell(new jxl.write.Number(5, rowCount, aBook.getPrice()));
			sheet.addCell(new jxl.write.Number(6, rowCount, aBook.getPrice()));
			rowCount++;
		}
	}

}